package com.github.pig.admin.controller.entranceguard;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.model.zhgl.building.CommunityBuilding;
import com.github.pig.admin.model.zhgl.community.CommunityInfo;
import com.github.pig.admin.model.zhgl.unit.BuildingUnit;
import com.github.pig.admin.service.zhgl.building.ICommunityBuildingService;
import com.github.pig.admin.service.zhgl.community.ICommunityInfoService;
import com.github.pig.admin.service.zhgl.house.IUnitHouseService;
import com.github.pig.admin.service.zhgl.unit.IBuildingUnitService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器-业主绑定小区
 * </p>
 *
 * @author xiaosa
 * @since 2018-06-04
 */
@Controller
@RequestMapping("/bindOwner")
public class BindOwnerController extends BaseController {
    @Autowired
    private ICommunityInfoService communityInfoService;
    @Autowired
    private ICommunityBuildingService communityBuildingService;
    @Autowired
    private IBuildingUnitService unitService;
    @Autowired
    private IUnitHouseService houseService;

    /**
     * 获取小区列表
     * @return 小区列表
     */
    @GetMapping("/communityList")
    @ResponseBody
    public List<CommunityInfo> communityInfoList() {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("del_flag",CommonConstant.STATUS_NORMAL);
        entityWrapper.orderBy("sort",true);
        return communityInfoService.selectList(entityWrapper);
    }

    /**
     * 根据小区获取楼宇列表
     * @return
     */
    @GetMapping("/buildingList")
    @ResponseBody
    public List<CommunityBuilding> buildingList(String communityId) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("community_id",communityId);
        entityWrapper.eq("del_flag",CommonConstant.STATUS_NORMAL);
        entityWrapper.orderBy("sort",true);
        return communityBuildingService.selectList(entityWrapper);
    }

    /**
     * 根据楼宇获取单元列表或房屋列表
     * @return
     */
    @GetMapping("/unitList")
    @ResponseBody
    public AjaxJson unitList(String buildingId) {
        AjaxJson j = new AjaxJson();
        Map<String,Object> map = new HashMap<>();
        CommunityBuilding building = communityBuildingService.selectById(buildingId);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("building_id",buildingId);
        entityWrapper.eq("del_flag", CommonConstant.STATUS_NORMAL);
        entityWrapper.orderBy("sort",true);
        if(building.getExistUnit()!=null&&"0".equalsIgnoreCase(building.getExistUnit())){
            map.put("existUnit","0");
            j.setAttributes(map);
            j.setObj(houseService.selectList(entityWrapper));

        }else{
            map.put("existUnit","1");
            j.setAttributes(map);
            j.setObj(unitService.selectList(entityWrapper));
        }
        return j;
    }

    @GetMapping("/getCommunityById")
    @ResponseBody
    public CommunityInfo getCommunityById(String id) {
        return communityInfoService.selectById(id);
    }

    @GetMapping("/getBuildingById")
    @ResponseBody
    public CommunityBuilding getBuildingById(String id) {
        return communityBuildingService.selectById(id);
    }

    @GetMapping("/getUnitById")
    @ResponseBody
    public BuildingUnit getUnitById(String id) {
        return unitService.selectById(id);
    }

}
